/** 
 * 手机号验证
 **/
export const checkPhone = (val) => {
  if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(val))) {
    // alert("手机号码有误，请重填");  
    return false;
  } else {
    return true
  }
}

/**
 * 固定电话验证
 * */

export const checkTel = (val) => {
  if (!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(val)) {
    // alert('固定电话有误，请重填');
    return false;
  } else {
    return true
  }
}

/**
 * 身份证验证
 * */
export function regIdCard(idcode) {
  console.log(idcode)
  const weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  const check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  const idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
  const format = idcard_patter.test(idcode);
  const seventeen = idcode.substring(0, 17);
  let num = 0;
  for (let i = 0; i < seventeen.length; i++) {
    num = num + seventeen[i] * weight_factor[i];
  }
  return idcode[17] === check_code[num % 11] && format ? true : false;
}

/**
 * 
 * 秒钟转化为时分秒
 * */
export function formatSeconds(value) {
  let result = parseInt(value)
  let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));

  let res = '';
  if (h !== '00') res += `${h}h`;
  if (m !== '00') res += `${m}min`;
  res = `${h}:${m}:${s}`
  return res;
}